/*********************************************************
	Project:		
	File name:	clock.c
	Description:	
	Write/Modify:	
	Time:		
*********************************************************/
#include "public.h"

#pragma section (fcode)



/*********************************************
	Function:		
	Description:	
	Write/Modify:	apollo
	Time:	
	//100ms timer interrupt entry
*********************************************/
static void _100ms_Timer(void)	
{
#ifdef SUPPORT_RTC

	n1stick++;
	if(n1stick==5)//500ms
		F_HalfSecond=1;
	if(n1stick>=10)
	{
		n1stick=0;
		if(USB_reset_detect_Timer<60)
		{
		    USB_reset_detect_Timer++;
		}
 		F_HalfSecond=0;
#if  SUPPORT_RDS
		RDSDataTimer1s();
#endif

	}

#endif
	if(F_HalfSecond!=F_HalfSecondBackup)
	{
		F_HalfSecondOut=1;
		F_HalfSecondBackup=F_HalfSecond;
		
	}
	else
		F_HalfSecondOut=0;
	if(nCloseLCDTimer)
		nCloseLCDTimer--;
	if(CameraTimer)
		CameraTimer--;
	if(AudioTimer>1) 
		--AudioTimer;
	if(BT_PowerCnt>1)
	    BT_PowerCnt--;
	F_PTY_Display=0;
	
}
/*********************************************
	Function:		
	Description:	
	Write/Modify:	apollo
	Time:	
	//10ms timer interrupt entry
*********************************************/
void  _10ms_Tick_Pro(void)
{
	if(Is_Machine_Power)
	{
#if  SUPPORT_RDS
		RDSDataTimer10ms();
#endif
		Radio10msTimer();
	}
#if  ENCODER_NUMBER>0
	if(Encoder_Scan_Timer[0])
		Encoder_Scan_Timer[0]--;
	if(Encoder_Scan_Timer[1])
		Encoder_Scan_Timer[1]--;
#endif
}
/*********************************************
	Function:		
	Description:	
	Write/Modify:	apollo
	Time:	
	_100ms Tick 
*********************************************/
void _100ms_Tick_Pro(void)
{
	_100ms_Timer();
	if(Is_Machine_Power)
	{
#if  SUPPORT_RDS
		RDSTimer100ms();
		RDSDataTimer100ms();
#endif
		Radio100msTimer();

	}
	Reset_Watch();
 #ifdef SUPPORT_BLUETOOTH
	SetAutoConnect();
#endif
}	
/*********************************************
	Function:		
	Description:	
	Write/Modify:	apollo
	Time:	
	//1S_Tick_Pro
*********************************************/
#ifndef SUPPORT_RTC

void _4ms_tick_clockset(void)
{

	n1stick++;
	if(n1stick==125)//500ms
		F_HalfSecond=1;
	if(n1stick>=249)
	{
	 	//if(F_2Minute_tick++<=3||n1stick>248)
	 	if(F_2Minute_tick++==0)
	//	if((FrontSource==SOURCE_TUNER&&Get_SysSetup_Flag==0&&Get_ScreenSetup_Flag==0)||n1stick>248)
	  	{
			n1stick=0;
			_1s_Tick_Pro();
			F_HalfSecond=0;
	  	}
		 //	if(F_2Minute_tick>=4)
		//F_2Minute_tick=0;
	}
	
}
#endif


/*********************************************
	Function:		
	Description:	
	Write/Modify:	apollo
	Time:	
	//1S_Tick_Pro
*********************************************/
void _1s_Tick_Pro(void)
{
#ifndef SUPPORT_RTC
 	if(AccOff_Slow_Mode==0)
	{
		Clock.sec++;
	}
	else 
		Clock.sec+=4;
	
	if(Clock.sec>=60)
//	if(Clock.sec>=59)
	{
		Clock.sec=0;
		Clock.min++;
		
	}	
		if(Clock.min>=60)
		{
			Clock.min=0;
			Clock.hour++;
	}
	if(Clock.hour>=24)
	{
		Clock.hour=0;
 		Clock.date++;
		if(Clock.date>getDayNumber(Clock.year,Clock.month))
		{
			Clock.date=1;
			Clock.month++;
		}
		if(Clock.month>12)
		{
			Clock.month=1;
			Clock.year++;
		}
	}

#endif
#if  SUPPORT_RDS
	RDSDataTimer1s();
#endif


	if(Is_Machine_Power)
	{
#ifndef SUPPORT_RTC

	if(clock_minold!=Clock.min)
	{

		PostMessageMutex(NAVI_MODULE,EVT_NAVI_RADIOTIME,0);

		if(FrontSource==SOURCE_USB||FrontSource==SOURCE_SD||RearSource==SOURCE_USB||RearSource==SOURCE_SD
			||(F_MPEG_VIDEO_PLAY==0&&(FrontSource==SOURCE_DVD||RearSource==SOURCE_DVD)))
	 		PostMessage(SPI_MODULE,SPI_TX_CLK,0);
		clock_minold=Clock.min;
	}
#endif
 		QueryTunerStatus();
	}

}
/*********************************************
	Function:		ClockChange
	Description:	
	Write/Modify:	apollo
	Time:		2005-1-12- 14:10:21
*********************************************/
void ClockChange(unsigned char hour,unsigned char min)
{
	uchar Min_bake=0,Hour_Bake=0,clock_chg=0;
	if(hour!= HOUR_KEEP)
	{
		Hour_Bake=Clock.hour;
     	if(hour== HOUR_UP)	  
		{
			Clock.hour=MaxMinChangeProtect(Clock.hour, 1, 23, 0, 0);
    	}	
		else if(hour== HOUR_DOWN)
		{
			Clock.hour=MaxMinChangeProtect(Clock.hour, -1, 23, 0, 0);
		}	
		else	
			Clock.hour=hour;	
		if(FrontSource==SOURCE_TUNER&&Clock.hour!=Hour_Bake)
			clock_chg=1;
	 		
	}	
	if(min!= MIN_KEEP) 
	{
		Min_bake=Clock.min;
		if(min== MIN_UP)
		{
			Clock.min=MaxMinChangeProtect(Clock.min, 1, 59, 0, 0);
		}	
		else if(min== MIN_DOWN)	
		{
			Clock.min=MaxMinChangeProtect(Clock.min, -1, 59, 0, 0);
		}	
		else
			Clock.min=min;	

		if(FrontSource==SOURCE_TUNER&&Clock.min!=Min_bake)
	 		clock_chg=1;
	}	
	Clock.sec=0;
	if(clock_chg)
	{
		PostMessageMutex(NAVI_MODULE,EVT_NAVI_RADIOTIME,0);
	}
}
#ifdef D_DATE_CALCDAY

const uchar   bMonthTable[12] = {1,4,4,7,2,5,7,3,6,1,4,6};
uchar GetClockDay(int wYear,unsigned char bMonth,unsigned char bDate)
{
	int  ret;

	ret = wYear +((wYear-1)/4)-((wYear-1)/100) +((wYear-1)/400);
	ret += bMonthTable[bMonth-1] ;
	ret += ((bMonth>2)&&_IsLeapYear(wYear))?1:0;

	ret += bDate +5;
	ret %= 7;	
	if(0 == ret)
		ret = 7;

	return((uchar)ret);

}
#endif
 uchar  days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 

uchar getDayNumber(uchar year_up2000, uchar month) 
{ 
	int year =2000+year_up2000;
	if (2 == month && 0 == (year % 4) && (0 != (year % 100) || 0 == (year % 400)))
		{   days[1] = 29;    }        
	return (days[month - 1]);
} 


#pragma section ()


